package com.itheima.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dto.Page;
import com.itheima.dto.RoleDTO;
import com.itheima.entity.MenuChildren;
import com.itheima.entity.Role;
import com.itheima.entity.RoleMenu;
import com.itheima.mapper.SysMenuMapper;
import com.itheima.mapper.SysRoleMapper;
import com.itheima.result.PageData;
import com.itheima.service.SysRoleService;
import com.itheima.vo.MenuChildrenVO;
import com.itheima.vo.RoleAuthListVO;
import com.itheima.vo.RoleMenuVO;
import com.itheima.vo.RoleVO;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @projectName: kafka
 * @package: com.itheima.service.impl
 * @className: SysRoleServiceImpl
 * @author: Eric
 * @description: TODO
 * @date: 2023/9/21 18:57
 * @version: 1.0
 */
@Service
@RequiredArgsConstructor
public class SysRoleServiceImpl implements SysRoleService {
    private final SysRoleMapper sysRoleMapper;

    private final SysMenuMapper sysMenuMapper;

    @Override
    public PageData pageQueryRole(Page page) {
        PageHelper.startPage(page.getPage(), page.getLimit());
        List<Role> roleList = sysRoleMapper.pageQueryRole(page.getName());
        PageInfo<Role> pageInfo = new PageInfo<>(roleList);

        List<RoleVO> roleVOSList = new ArrayList<>();
        for (Role role : roleList) {
            String roleMenus = sysRoleMapper.selectRoleMenusById(role.getId());
            RoleVO roleVO = new RoleVO();
            BeanUtils.copyProperties(role,roleVO);
            roleVO.setCreate_date(role.getCreateDate());
            roleVO.setUpdate_date(role.getUpdateDate());
            roleVO.setDel_flag(role.getDelFlag());
            roleVO.setDefault_data(role.getDefaultData());
            roleVO.setRole_id(role.getId());
            roleVO.setRole_menus(roleMenus);
            roleVOSList.add(roleVO);
        }
        return new PageData(pageInfo.getTotal(),
                pageInfo.getPages(),
                pageInfo.getPageSize(),
                pageInfo.getPageNum(),
                roleVOSList);
    }

    @Override
    public List<RoleVO> queryAllRoles() {
        List<Role> roleList = sysRoleMapper.queryAllRoles();
        List<RoleVO> roleVOList = new ArrayList<>();
        for (Role role : roleList) {
            RoleVO roleVO = new RoleVO();
            BeanUtils.copyProperties(role,roleVO);
            roleVO.setCreate_date(role.getCreateDate());
            roleVO.setUpdate_date(role.getUpdateDate());
            roleVO.setDel_flag(role.getDelFlag());
            roleVO.setDefault_data(role.getDefaultData());
            roleVOList.add(roleVO);
        }
        BeanUtils.copyProperties(roleList,roleVOList);
        return roleVOList;
    }

    @Override
    @Transactional
    public void creatRole(RoleDTO roleDTO) {
        Integer countName = sysRoleMapper.selectCountByName(roleDTO.getName());
        if (countName>0){
            throw new RuntimeException("角色名重复!");
        }
        Role role = new Role();
        BeanUtils.copyProperties(roleDTO,role);
        role.setId(UUID.randomUUID().toString());
        role.setDefaultData("1");
        role.setDelFlag("0");
        role.setSuperadmin(0);
        sysRoleMapper.creatRole(role);
        List<Integer> menuids = roleDTO.getMenuids();
        sysRoleMapper.creatRoleMenuid(role.getId(),menuids);
    }

    @Override
    @Transactional
    public void deleteRole(String id) {
        sysRoleMapper.deleteRole(id);
        sysRoleMapper.deleteRoleMenu(id);
    }

    @Override
    public RoleAuthListVO queryRole(String id) {
        Role role = sysRoleMapper.selectRoleById(id);
        RoleAuthListVO roleAuthListVO = new RoleAuthListVO();
        BeanUtils.copyProperties(role,roleAuthListVO);
        roleAuthListVO.setCreate_date(role.getCreateDate());
        roleAuthListVO.setUpdate_date(role.getUpdateDate());
        roleAuthListVO.setDel_flag(role.getDelFlag());
        roleAuthListVO.setDefault_data(role.getDefaultData());

        List<MenuChildren> firstMenus = sysRoleMapper.selectByFatherPid(0,id);
        List<MenuChildrenVO> menuChildrenVOList1 = new ArrayList<>();
        firstMenus.forEach(c->{
            List<MenuChildren> secondMenus = sysRoleMapper.selectByFatherPid(c.getId(),id);
            List<MenuChildrenVO> menuChildrenVOList2 = new ArrayList<>();
            secondMenus.forEach(c1->{
                List<MenuChildren> thirdMenus = sysRoleMapper.selectByFatherPid(c1.getId(),id);
                c1.setChildren(thirdMenus);
                MenuChildrenVO menuChildrenVO2 = new MenuChildrenVO();
                BeanUtils.copyProperties(c1,menuChildrenVO2);
                menuChildrenVO2.setIs_show(c1.getIsShow());
                menuChildrenVO2.setIs_cache(c1.getIsCache());
                menuChildrenVO2.setIs_link(c1.getIsLink());
                menuChildrenVO2.setDel_flag(c1.getDelFlag());
                List<MenuChildrenVO> menuChildrenVOList3 = new ArrayList<>();
                thirdMenus.forEach(c2 ->{
                    MenuChildrenVO menuChildrenVO3 = new MenuChildrenVO();
                    BeanUtils.copyProperties(c2,menuChildrenVO3);
                    menuChildrenVO3.setIs_show(c2.getIsShow());
                    menuChildrenVO3.setIs_cache(c2.getIsCache());
                    menuChildrenVO3.setIs_link(c2.getIsLink());
                    menuChildrenVO3.setDel_flag(c2.getDelFlag());
                    menuChildrenVO2.setChildren(null);
                    menuChildrenVOList3.add(menuChildrenVO3);
                });
                menuChildrenVO2.setChildren(menuChildrenVOList3);
                menuChildrenVOList2.add(menuChildrenVO2);
            });

            MenuChildrenVO menuChildrenVO1 = new MenuChildrenVO();
            BeanUtils.copyProperties(c,menuChildrenVO1);
            menuChildrenVO1.setIs_show(c.getIsShow());
            menuChildrenVO1.setIs_cache(c.getIsCache());
            menuChildrenVO1.setIs_link(c.getIsLink());
            menuChildrenVO1.setDel_flag(c.getDelFlag());
            menuChildrenVO1.setChildren(menuChildrenVOList2);
            menuChildrenVOList1.add(menuChildrenVO1);
        });
        roleAuthListVO.setAuthList(menuChildrenVOList1);
        return roleAuthListVO;
    }

    @Override
    @Transactional
    public void updateRole(RoleDTO roleDTO) {
        Integer countName = sysRoleMapper.selectExistsCountByName(roleDTO);
        if (countName>0){
            throw new RuntimeException("角色名重复!");
        }
        Role role = new Role();
        BeanUtils.copyProperties(roleDTO,role);
        sysRoleMapper.updateRole(role);
        List<Integer> menuids = roleDTO.getMenuids();
        sysRoleMapper.deleteRoleMenu(role.getId());
        sysRoleMapper.creatRoleMenuid(role.getId(),menuids);
    }
}
